package com.diandian.backend.product.service;

import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import com.diandian.backend.product.domain.ProductOfflineStock;
import com.diandian.backend.product.dto.*;
import com.diandian.backend.product.mapper.ProductOfflineStockMapper;
import com.diandian.backend.product.resp.ProductOfflineStockDetailResp;
import com.diandian.backend.product.resp.ProductOfflineStockPageResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.diandian.backend.product.resp.ProductOfflineStockListResp;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductOfflineStockService {

    @Autowired
    private ProductOfflineStockMapper productOfflineStockMapper;

    public PageResult page(ProductOfflineStockPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = productOfflineStockMapper.getCount(requestDTO);

        // 查询数据集合
        List<ProductOfflineStock> dataList = productOfflineStockMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<ProductOfflineStockPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){

            // todo 增加门店名称和商品名称查询
            dataList.stream().forEach( x ->{
                ProductOfflineStockPageResp item = new ProductOfflineStockPageResp();
                item.setId(x.getId());
                item.setProductCode(x.getProductCode());
                item.setSkuCode(x.getSkuCode());
                item.setStoreCode(x.getStoreCode());
                item.setStockCount(x.getStockCount());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<ProductOfflineStockListResp> getList(ProductOfflineStockListParamDTO requestDTO){

        // 查询数据集合
        List<ProductOfflineStock> dataList = productOfflineStockMapper.getList(requestDTO);

        // 组装数据
        List<ProductOfflineStockListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                ProductOfflineStockListResp item = new ProductOfflineStockListResp();
                item.setId(x.getId());
                item.setProductCode(x.getProductCode());
                item.setSkuCode(x.getSkuCode());
                item.setStoreCode(x.getStoreCode());
                item.setStockCount(x.getStockCount());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public ProductOfflineStockDetailResp getDetail(ProductOfflineStockDetailParamDTO param) {

        ProductOfflineStock dbData = productOfflineStockMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        ProductOfflineStockDetailResp result = new ProductOfflineStockDetailResp();
        result.setId(dbData.getId());
        result.setProductCode(dbData.getProductCode());
        result.setSkuCode(dbData.getSkuCode());
        result.setStoreCode(dbData.getStoreCode());
        result.setStockCount(dbData.getStockCount());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(ProductOfflineStockUpdateByIdParamDTO param) {
        param.check();

        ProductOfflineStockDetailParamDTO queryDetailDTO = new ProductOfflineStockDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        queryDetailDTO.setBelong(param.getBelong());
        queryDetailDTO.setOrgId(param.getOrgId());
        queryDetailDTO.setOrgCode(param.getOrgCode());
        ProductOfflineStock dbData = productOfflineStockMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("记录不存在");
        }

        productOfflineStockMapper.updateById(param);
    }

    public void deleteById(ProductOfflineStockDeleteByIdParamDTO paramDTO) {

        paramDTO.check();

        ProductOfflineStockDetailParamDTO queryDetailDTO = new ProductOfflineStockDetailParamDTO();
        queryDetailDTO.setId(paramDTO.getId());
        queryDetailDTO.setBelong(paramDTO.getBelong());
        queryDetailDTO.setOrgId(paramDTO.getOrgId());
        queryDetailDTO.setOrgCode(paramDTO.getOrgCode());
        ProductOfflineStock dbData = productOfflineStockMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("记录不存在");
        }

        productOfflineStockMapper.deleteById(paramDTO);
    }

    public void add(ProductOfflineStockAddParamDTO param) {

        ProductOfflineStockDetailParamDTO queryDetailDTO = new ProductOfflineStockDetailParamDTO();
        queryDetailDTO.setStoreCode(param.getStoreCode());
        queryDetailDTO.setProductCode(param.getProductCode());
        queryDetailDTO.setSkuCode(param.getSkuCode());
        ProductOfflineStock dbData = productOfflineStockMapper.getDetail(queryDetailDTO);
        if(dbData != null){
            throw new DefaultException("已存在此库存记录");
        }

        param.setDel(0);
        productOfflineStockMapper.add(param);
    }
}
